package com.fuershiye.woman.monitor.controller;

import java.util.List;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fuershiye.woman.monitor.model.DepartmentType;
import com.fuershiye.woman.monitor.service.DepartmentTypeService;

/**
 * 部门的管理
 * 
 * @author yongyongdong
 *
 */
@Controller
@RequestMapping(value = "/department/type")
public class DepartmentTypeController {
	
	private static final Logger logger = LoggerFactory.getLogger(DepartmentTypeController.class);
	
    @Autowired
    DepartmentTypeService departmentTypeService;
    
//    @RequiresPermissions("departmentType:view")
    @RequestMapping(value="/view.do")
    public String view(String page){
        return "department/" + page;
    }

//    @RequiresPermissions("departmentType:list")
    @RequestMapping(value="/list.do")
    @ResponseBody
    public List<DepartmentType> list(String name) {
    	return departmentTypeService.list(name);
    }
    
//    @RequiresPermissions("departmentType:save")
    @RequestMapping(value = "/save.do")
    @ResponseBody
    public boolean save(Long id, String code, String name) {
    	DepartmentType dt = null;
    	boolean add = false;
    	if(id==null || id==0){
    		add = true;
    		dt = new DepartmentType();
    	}else{
    		dt = departmentTypeService.get(id);
    		if(dt==null){
        		logger.error("DepartmentTypeController update,DepartmentType is null");
            	return false;
        	}
    	}
    	
    	dt.setCode(code);
    	dt.setName(name);
    	dt.setStatus(1);
    	departmentTypeService.save(dt);
    	
    	//如果是新加的则给默认的排序号sort(主键id)
    	if(add){
    		DepartmentType top = departmentTypeService.getTop();
    		if(top!=null){
    			dt.setSort(top.getId());
    			departmentTypeService.save(dt);
    			return true;
    		}else{
    			logger.error("DepartmentTypeController add is null");
    			return false;
    		}
    	}
    	return true;
    }
    
    /**
     * @param id	要排序的数据
     * @param aim	1:升序; 2:降序
     * @return
     */
//    @RequiresPermissions("departmentType:sort")
    @RequestMapping(value = "/sort.do")
    @ResponseBody
    public boolean sort(Long id, Integer aim) {
    	
    	if(id==null||(aim!=1&&aim!=2)){
        	logger.info("DepartmentTypeController sort id null, id:{},aim:{}",id,aim);
    		return false;
    	}
    	DepartmentType it = departmentTypeService.get(id);
    	if(it==null){
        	logger.error("DepartmentTypeController DepartmentType null, id:{}",id);
    		return false;
    	}
    	
    	DepartmentType top = departmentTypeService.findBySort(it.getSort(),aim,1);
		if(top==null){
			//查不到需要交换的对象,则认为交换成功
    		return true;
    	}
		//交换序号
		long temp = it.getSort();
		it.setSort(top.getSort());
		top.setSort(temp);
		//保存
		departmentTypeService.save(it);
		departmentTypeService.save(top);
    	return true;
    }
    
//    @RequiresPermissions("departmentType:delete")
    @RequestMapping(value="/delete.do")
    @ResponseBody
    public boolean delete(Long id) {
    	DepartmentType dt = departmentTypeService.get(id);
    	dt.setStatus(0);//修改为已删除
    	departmentTypeService.save(dt);
        return true;
    }

}
